<?php

class cntrlmanagebook implements viewController {

	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js", "manageBook.js"), "CSS" => array("facebox.css"));
	private $pageName = "";
	private $userObj;
	private $thisID;
	
	public function __construct(user &$userObj){

		if( $userObj->getAccessLevel() == -1 ){
			header("Location: home");
			exit;
		}
		
		$this->side = new template('control.manageBook');
		$this->area = new template('area.manageBook');

		$this->userObj = $userObj;
		
		parse_str( $_GET['act'] );

		$bookInfo = book::getBook($userObj, $id);

		$this->thisID = $id;


		$this->renderPage($id, $bookInfo);
		$this->renderBookList();
	}

	private function renderPage($id, $bookInfo){
		$id = (int)$id;		
		
		$this->area->replaceAll(array(
			"groupName" => $bookInfo['name'],
			"groupDescription" => $bookInfo['description'],
			"bID" => $bookInfo['bID']
		));
		
		
		// Get a list of managed groups that are not in the list in this group.
	
		$toAdd = "";
		$managedGroups = $this->userObj->getManagedGroups(true);
		
		foreach($managedGroups as $myGroup){
			$toAdd .= "<option value='" . $myGroup['gID'] . "'>" . $myGroup['groupName'] . "</option>\n";
		}
		
		// Get all clones of this book.
		
		$clones = book::getClones($this->userObj, $id, book::LOCATION_GROUP);
		
		$select = "";
		foreach($clones as $clone){
			$select .= "<option value='" . $clone['group_gID'] . "'>" . $clone['name'] . "</option>\n";
		}		
		
		$this->area->replace('retrieveSelect', $select);
		$this->area->replace('donateSelect', $toAdd);
		
		// Is this book in a group?
		if($bookInfo['group_gID'] != ""){
			// Add the option to save a copy in the current group
			$this->area->replace('saveoptionGroup', '<input type="radio" name="copyLocation" value="same" id="sameLibrary" />	<label for="sameLibrary"><var id="lang_318_saveSameGroup"></var></label><br />');
		} else {
			$this->area->replace('saveoptionGroup', '');
		}
		
	}

	protected function renderBookList(){
		$books = book::getBookList($this->userObj->getuID(), 0, 500);
	
		$output = "";
		
		foreach($books as $book){
			if($this->thisID == $book['bID']){
				$output .= "<option value='" . $book['bID'] . "' selected='selected'>" . $book['name'] . "</option>\n";
			} else {
				$output .= "<option value='" . $book['bID'] . "'>" . $book['name'] . "</option>\n";
			}
		}
		
		$this->side->replace('groupList', $output);
	}

	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return "";
	}

}

?>